<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb class="navigation">
      <el-breadcrumb-item :to="{ path: '/loginlog' }">系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>登录日志</el-breadcrumb-item>
    </el-breadcrumb>
  <!-- 主体区域 -->
    <el-card class="notice">
        <h4>&nbsp;&nbsp;登录日志</h4>
      <!--搜索区域-->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入用户名称" v-model="queryInfo.txt">
            <el-button slot="append"
              icon="el-icon-search"
              @click="getUserLog"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button size="medium"
            type="primary"
            @click="deleteLoginLog()">
            清空日志
          </el-button>
        </el-col>
      </el-row>
      <!--文件数据区域-->
      <el-table :data="userlog"
        border
        stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="用户名称" prop="username"></el-table-column>
        <el-table-column label="登录时间" prop="logintime"></el-table-column>
        <el-table-column label="登录信息" prop="message"></el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        // 当前页码
        pageNum: 1,
        pageSize: 5,
        txt: ''
      },
      total: 0,
      userlog: []
    }
  },
  created () {
    this.getUserLog()
  },
  methods: {
    getUserLog () {
      this.$axios.get('admin/pogLoginlog/searchbypage', {
        params: this.queryInfo
      }).then(res => {
        const info = res.data
        if (info.code === 401) {
          console.log(info)
          this.$message({
            message: '获取文件数据失败',
            type: 'error',
            offset: 70
          })
        }
        this.userlog = info.data.list
        this.total = info.data.total
      }).catch(err => {
        console.log(err)
      })
    },
    // 弹框询问
    async deleteLoginLog () {
      const confirmResult = await this.$confirm(
        '此操作将清空登录日志, 是否继续?', '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => {
        return err
      })
      // 如果用户确认删除，则返回值为字符串confirm
      // 如果用户确认删除，则返回值为字符串cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$axios.delete('admin/pogLoginlog')
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('清空日志失败！')
      }
      this.$message.success('清空日志成功！')
      this.getUserLog()
    },
    // 监听pagesize改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getUserLog()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getUserLog()
    }
  }
}
</script>
<style  scoped>
.navigation {
  padding: 10px;
}
.notice {
  margin-bottom: 40px;
  border-radius: 15px;
  box-shadow: 15px 15px 10px -12px #4c61a0;
}
h4 {
  padding-bottom: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid #4c61a0;
}
.el-table {
    margin-top: 15px;
    font-size: 14px;
}
/* 页码 */
.el-pagination {
  margin-top: 15px;
}
</style>
